from tkinter import *
from bs4 import BeautifulSoup
import win32clipboard 
import win32con
import requests
import sys,re,time

#UI-PY3
#2014.5
class win:
	def __init__(self,):
		#载入需求库
		self.clip=win32clipboard
		#主窗口
		self.top=Tk()
		self.top.title('通用窗口')
		self.top.bind("<Return>",self.quit)
		#self.top.geometry('500x300')
		#组件
		self.frame=Frame(width=400,height=2,bg='red')
		self.frame.grid(row=0,column=0)
		
		self.label=Label(self.top,text='空格开始，回车结束')
		self.label.grid(row=0,column=0)
		
		self.entry=Text(self.top,width=60,height=1)
		self.entry.insert(1.0,'复制帖子URL到剪贴板，按空格开始')
		self.entry.grid(row=1,column=0)
		
		self.tx=Text(self.top,width=60,height=10)
		self.tx.grid(row=2,column=0)
		
		self.button=Button(self.top,text='开始',width=60)
		self.button.bind('<space>',self.run)
		self.button.bind('<Button-1>',self.run)
		self.button.focus_set()
		self.button.grid(row=3,column=0)
		
		#退出
	def quit(self,event):
		self.top.destroy()
		
		#标准输出		
	def prt(self,txts):
		self.tx.insert(1.0,'%s\n'%txts)
		#输入处理
	def iput(self):
		self.clip.OpenClipboard()
		txs=self.clip.GetClipboardData(win32con.CF_TEXT)
		self.entry.insert(1.0,'%s\n'%txs.decode('GBK'))
		self.clip.CloseClipboard()
		return txs.decode('GBK')
	def bsurl(self,url):
		rq=requests.get(url)
		self.prt('请求中。。。')
		time.sleep(1)
		bsrq=BeautifulSoup(rq.text)
		self.prt('BS4转换中。。。')
		
		
		#开始
	def run(self,event):
		self.url=self.iput() #输入处理
		self.btxs=self.bsurl(self.url) #BS4转换
		
if __name__=='__main__':
	win=win()
	mainloop()